package com.imooc.ajaxserver2;

import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @date 2018-3-7
 */
@RestController
@RequestMapping("/test")
/**
 * 1.支出跨域的注解，可以放在类上也可以放在方法上,
 * allowCredentials=true:支持带cookie的跨域
 */
@CrossOrigin(allowCredentials = "true")
// 2.还可以通过接口编程实现跨域：实现WebMvcConfigurer接口，并实现接口的addCorsMapping方法
public class TestController {
    @GetMapping("/get1")
    public ResultBean get1() {
        System.out.println("testController.get1()");
        return new ResultBean("i'm date");
    }

    @PostMapping("/getJson")
    public User getJson(@RequestBody User user) {
        System.out.println(user);
        user.setAge(user.getAge() + 2);
        return user;
    }

    @PostMapping("/getCookie")
    public ResultBean getCookie(@CookieValue(value = "myCookie") String myCookie) {
        System.out.println(myCookie);
        return new ResultBean(myCookie);
    }

    @GetMapping("/getHeader")
    public ResultBean getHeader(@RequestHeader("x-header1") String header1, @RequestHeader("x-header2") String header2) {
        System.out.println("getHeader " + header1 + " " + header2);
        return new ResultBean("getHeader " + header1 + " " + header2);
    }
}
